package com.dx.mes.service.brm.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_SWH;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_WHOLE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_NEW;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_SWH_WHOLE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPBISPTH_ALREADY_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPBISPTH_ITEM_ALREADY_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPBISPTH_ITEM_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPBISPTH_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.PATH_TYPE_MAIN;
import static com.dx.mes.common.bean.def.GenericDef.PATH_TYPE_REWK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FBPBISPTH;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.dao.IBisOpeDao;
import com.dx.mes.common.dao.IBisPathDao;
import com.dx.mes.common.dao.IBisPathSwhDao;
import com.dx.mes.common.dao.IBisPathiDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.brm.service.IFbpbispathService;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthI;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthIA;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthIB;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthO;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthOA;
import com.dx.mes.facade.brm.tx.Fbpbispth.FbpbispthOB;

@Service("fbpbispathService")
public class FbpbispathService implements IFbpbispathService {
	private static final String TX_NAME = T_FBPBISPTH;	
	private LogUtils logUtils;
	
	@Autowired
	private IBisPathDao bisPathDao;

	@Autowired
	private IBisPathiDao bisPathiDao;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;

	@Autowired
	private IBisPathSwhDao bisPathSwhDao;
	
	@Autowired
	private IBisOpeDao bisOpeDao;

	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpbispathService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpbispathService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String strOutTrx = "";
		Session session = bisPathDao.getCrSession();

		FbpbispthI inTrx = new FbpbispthI();
		FbpbispthO outTrx = new FbpbispthO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
		
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FbpbispthI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition(inTrx,outTrx);
				break;
			case ACTION_FLG_ADD:
				save(inTrx,outTrx);
				break;
			case ACTION_FLG_UPDATE:
				updateFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_DELETE:
				deleteFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_INQUIRE:
				queryPathItemFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_NEW:
				addORUpdatePathItemFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_LIST_WHOLE:
				listWholeFnc(outTrx);
				break;
			case ACTION_FLG_LIST_SWH:
				querySwhPath(outTrx);
				break;
			case ACTION_FLG_SWH_WHOLE:
				queryWholeSwhPath(outTrx);
				break;
			case 'O':
				updateOkOrHoldByCondition(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	public void save(FbpbispthI inTrx,FbpbispthO outTrx){
		addFnc(inTrx,outTrx);
	}
	
	public void queryByCondition(FbpbispthI inTrx,FbpbispthO outTrx) {
		if (inTrx.getIaryA() == null) {
			return;
		}
		FbpbispthIA iaryA = inTrx.getIaryA().get(0);
		List<FbpbispthOA> oaryAList = new ArrayList<FbpbispthOA>();
		List<FbpbispthOB> oaryBList = new ArrayList<FbpbispthOB>();
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		String path_cate = iaryA.getPath_cate();
		String hql = "FROM Bis_pathi where 1=1 ";
		if (path_id != null) {
			if (path_id.indexOf('*') == -1) {
				hql = hql + " and id.path_id in ('" + path_id + "')";
			} else {
				hql = hql + " and id.path_id like '" + path_id.replace('*', '%') + "'";
			}
		}
		if (path_ver != null) {
			if (path_ver.indexOf('*') == -1) {
				hql = hql + " and id.path_ver='" + path_ver + "'";
			} else {
				hql = hql + " and id.path_ver like '" + path_ver.replace('*', '%') + "'";
			}
		}
		if (path_cate != null) {
			if (path_cate.indexOf('*') == -1) {
				hql = hql + " and path_cate='" + path_cate + "'";
			} else {
				hql = hql + " and path_cate like '" + path_cate.replace('*', '%') + "'";
			}
		}
		List<Bis_pathi> bisPathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : bisPathiList) {
			FbpbispthOA oaryA = new FbpbispthOA();
			oaryA.setPath_id(bis_pathi.getId().getPath_id());
			oaryA.setPath_ver(bis_pathi.getId().getPath_ver());
			oaryA.setPath_dsc(bis_pathi.getPath_dsc());
			oaryA.setPath_cate(bis_pathi.getPath_cate());
			oaryA.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oaryA.setPath_stat(bis_pathi.getPath_stat());
			oaryAList.add(oaryA);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
		if (outTrx.getTbl_cnt_a() > 0) {
			String hql_dt = "FROM Bis_path where id.path_id_fk = '" + outTrx.getOaryA().get(0).getPath_id()
					+ "' and id.path_ver_fk = '" + outTrx.getOaryA().get(0).getPath_ver() + "'";
			List<Bis_path> bisPathList = bisPathDao.find(hql_dt);
			for (Bis_path bis_path : bisPathList) {
				FbpbispthOB oaryB = new FbpbispthOB();
				oaryB.setPath_id_fk(bis_path.getId().getPath_id_fk());
				oaryB.setPath_ver_fk(bis_path.getId().getPath_ver_fk());
				oaryB.setCr_ope_no(bis_path.getId().getCr_ope_no());
				oaryB.setPv_ope_no(bis_path.getPv_ope_no());
				oaryB.setNx_ope_no(bis_path.getNx_ope_no());

				oaryB.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
				oaryB.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
				Bis_opeId  bis_opeId = new Bis_opeId();
				bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
				bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
				Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
				if(bis_ope!=null){
					oaryB.setCr_ope_desc(bis_ope.getOpe_dsc());
				}
				oaryB.setSwh_avl_flg(bis_path.getSwh_avl_flg());
				oaryB.setWip_bank_flg(bis_path.getWip_bank_flg());
				oaryB.setDft_wip_bank_tool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());
				oaryBList.add(oaryB);
			}
			outTrx.setOaryB(oaryBList);
			outTrx.setTbl_cnt_b(oaryBList.size());
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void addFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Timestamp evtTimeStamp = Util.getCurrentTimestamp();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi query_bis_pathi = bisPathiDao.get(id);
		if (query_bis_pathi != null) {
			outTrx.setRtn_code(E_FBPBISPTH_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("record already exist");
			return;
		}
		Bis_pathi bis_pathi = new Bis_pathi();
		bis_pathi.setId(id);
		bis_pathi.setPath_cate(iaryA.getPath_cate());
		bis_pathi.setPath_dsc(iaryA.getPath_dsc());
		bis_pathi.setFirst_ope_no(iaryA.getFirst_ope_no());
		bis_pathi.setEvt_usr(inTrx.getEvt_usr());
		bis_pathi.setPath_stat(iaryA.getPath_stat());
		bis_pathi.setEvt_timestamp(evtTimeStamp);
		bisPathiDao.save(bis_pathi);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	public void updateOkOrHoldByCondition(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIA iaryA = inTrx.getIaryA().get(0);
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if (bis_pathi == null) {
			outTrx.setRtn_code(E_FBPBISPTH_NOT_FOUND + "");
			outTrx.setRtn_mesg("record not found");
			return;
		}
		if (iaryA.getPath_stat()!= null) {
			bis_pathi.setPath_stat(iaryA.getPath_stat());
		}
		if (iaryA.getEvt_usr()!= null) {
			bis_pathi.setEvt_usr(iaryA.getEvt_usr());
			bis_pathi.setEvt_timestamp(current_timestamp);
		}
		bisPathiDao.update(bis_pathi);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	public void updateFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if (bis_pathi == null) {
			outTrx.setRtn_code(E_FBPBISPTH_NOT_FOUND + "");
			outTrx.setRtn_mesg("record not found");
			return;
		}
		if (iaryA.getPath_cate() != null) {
			bis_pathi.setPath_cate(iaryA.getPath_cate());
		}
		if (iaryA.getPath_dsc() != null) {
			bis_pathi.setPath_dsc(iaryA.getPath_dsc());
		}
		if (iaryA.getFirst_ope_no() != null) {
			bis_pathi.setFirst_ope_no(iaryA.getFirst_ope_no());
		}
		if(inTrx.getEvt_usr() != null){
			bis_pathi.setEvt_usr(inTrx.getEvt_usr());
		}
		bis_pathi.setPath_stat(iaryA.getPath_stat());
		bisPathiDao.update(bis_pathi);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void deleteFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIA iaryA = inTrx.getIaryA().get(0);
		String path_id = iaryA.getPath_id();
		String path_ver = iaryA.getPath_ver();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(path_id);
		id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if (bis_pathi == null) {
			outTrx.setRtn_code(E_FBPBISPTH_NOT_FOUND + "");
			outTrx.setRtn_mesg("record already exist");
			return;
		}
		bisPathiDao.delete(bis_pathi);
		// 将route 站点信息删除
		String hql = "FROM Bis_path where path_id_fk='" + path_id + "'" + " and path_ver_fk ='" + path_ver + "'";
		List<Bis_path> pathItemList = bisPathDao.find(hql);
		for (Bis_path bis_path : pathItemList) {
			bisPathDao.delete(bis_path);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void queryPathItemFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
//		queryByCondition(inTrx,outTrx);
		//test getIaryA().get(0)..get(0)
		FbpbispthIB iaryB = new FbpbispthIB();
		List<FbpbispthIB> iaryBList = inTrx.getIaryB();

		if(iaryBList!=null){
			iaryB= iaryBList.get(0);
		}else{
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg("工艺路线代码和版本为空");
			return;
		}
		
		List<FbpbispthOB> oaryBList = new ArrayList<FbpbispthOB>();
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id = iaryB.getPath_id_fk();
		String path_ver = iaryB.getPath_ver_fk();
		String hql = "FROM Bis_path where 1=1 ";
		if (StringUtils.isNotBlank(path_id)) {
			hql = hql + " and path_id_fk='" + path_id + "'";
		}
		if (StringUtils.isNotBlank(path_ver)) {
			hql = hql + " and path_ver_fk='" + path_ver + "'";
		}
		if (StringUtils.isNotBlank(cr_ope_no)) {
			if (cr_ope_no.indexOf('*') == -1) {
				hql = hql + " and cr_ope_no='" + cr_ope_no + "'";
			}else{ 
			hql = hql + " and cr_ope_no like '" + cr_ope_no.replace('*', '%') + "'";
			}
		}
		List<Bis_path> bisPathList = bisPathDao.find(hql);
		for (Bis_path bis_path : bisPathList) {
			FbpbispthOB oaryB = new FbpbispthOB();
			oaryB.setPath_id_fk(bis_path.getId().getPath_id_fk());
			oaryB.setPath_ver_fk(bis_path.getId().getPath_ver_fk());
			oaryB.setCr_ope_no(bis_path.getId().getCr_ope_no());
			oaryB.setPv_ope_no(bis_path.getPv_ope_no());
			oaryB.setNx_ope_no(bis_path.getNx_ope_no());
			oaryB.setCr_ope_id_fk(bis_path.getCr_ope_id_fk());
			oaryB.setCr_ope_ver_fk(bis_path.getCr_ope_ver_fk());
			oaryB.setSwh_avl_flg(bis_path.getSwh_avl_flg());
			oaryB.setWip_bank_flg(bis_path.getWip_bank_flg());
			oaryB.setDft_wip_bank_tool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());
			oaryB.setSkip_flg(bis_path.getSkip_flg());
			oaryBList.add(oaryB);
		}
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt_b(oaryBList.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void addORUpdatePathItemFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		// 删除route信息 重写insert
		String path_id = inTrx.getIaryB().get(0).getPath_id_fk();
		String path_ver = inTrx.getIaryB().get(0).getPath_ver_fk();
		Bis_pathiId pathi_id = new Bis_pathiId();
		pathi_id.setPath_id(path_id);
		pathi_id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(pathi_id);
		if( bis_pathi == null ){
			addFnc(inTrx,outTrx);
		}
		// 更新pathi.first_ope_no
		String first_ope_no = inTrx.getIaryA().get(0).getFirst_ope_no();
		bis_pathi.setFirst_ope_no(first_ope_no);
		bis_pathi.setEvt_usr(inTrx.getEvt_usr());
		bisPathiDao.update(bis_pathi);
		
//		将route 站点信息删除
		String hql = "FROM Bis_path where path_id_fk='" + path_id + "'" + " and path_ver_fk ='" + path_ver + "'";
		List<Bis_path> pathItemList = bisPathDao.find(hql);
		for (Bis_path bis_path : pathItemList) {
			bisPathDao.delete(bis_path);
		}
			 

		// 将站点信息重新写入path表
		long tbl_cnt_b = inTrx.getTbl_cnt_b();
		for (int i = 0; i < tbl_cnt_b; i++) {
			FbpbispthIB iaryB = inTrx.getIaryB().get(i);
			String cr_ope_no = iaryB.getCr_ope_no();
			String path_id_fk = iaryB.getPath_id_fk();
			String path_ver_fk = iaryB.getPath_ver_fk();
			Bis_pathId id = new Bis_pathId();
			id.setCr_ope_no(cr_ope_no);
			id.setPath_id_fk(path_id_fk);
			id.setPath_ver_fk(path_ver_fk);
			Bis_path query_bis_path = bisPathDao.get(id);
			if (query_bis_path != null) {
				outTrx.setRtn_code(E_FBPBISPTH_ITEM_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("recorder already exist");
				return;
			}
			Timestamp current_timestamp = Util.getCurrentTimestamp();
			Bis_path bis_path = new Bis_path();
			bis_path.setId(id);
			bis_path.setCr_ope_id_fk(iaryB.getCr_ope_id_fk());
			bis_path.setCr_ope_ver_fk(iaryB.getCr_ope_ver_fk());
			bis_path.setDft_wip_bank_tool_id_fk(iaryB.getDft_wip_bank_tool_id_fk());
			bis_path.setNx_ope_no(iaryB.getNx_ope_no());
			bis_path.setPv_ope_no(iaryB.getPv_ope_no());
			bis_path.setSwh_avl_flg(iaryB.getSwh_avl_flg());
			bis_path.setWip_bank_flg(iaryB.getWip_bank_flg());
			bis_path.setSkip_flg(iaryB.getSkip_flg());
			bis_path.setEvt_usr(inTrx.getEvt_usr());
			bis_path.setEvt_timestamp(current_timestamp);
			bisPathDao.save(bis_path);
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);

	}

	public void updatePathItemFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIB iaryB = inTrx.getIaryA().get(0).getIaryB().get(0);
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id_fk = iaryB.getPath_id_fk();
		String path_ver_fk = iaryB.getPath_ver_fk();
		Bis_pathId id = new Bis_pathId();
		id.setCr_ope_no(cr_ope_no);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		Bis_path bis_path = bisPathDao.get(id);
		if (bis_path != null) {
			outTrx.setRtn_code(E_FBPBISPTH_ITEM_NOT_FOUND + "");
			outTrx.setRtn_mesg("recorder already exist");
			return;
		}
		if (iaryB.getCr_ope_id_fk() != null) {
			bis_path.setCr_ope_id_fk(iaryB.getCr_ope_id_fk());
		}
		if (iaryB.getCr_ope_ver_fk() != null) {
			bis_path.setCr_ope_ver_fk(iaryB.getCr_ope_ver_fk());
		}
		if (iaryB.getDft_wip_bank_tool_id_fk() != null) {
			bis_path.setDft_wip_bank_tool_id_fk(iaryB.getDft_wip_bank_tool_id_fk());
		}
		if (iaryB.getNx_ope_no() != null) {
			bis_path.setNx_ope_no(iaryB.getNx_ope_no());
		}
		if (iaryB.getPv_ope_no() != null) {
			bis_path.setPv_ope_no(iaryB.getPv_ope_no());
		}
		if (iaryB.getSwh_avl_flg() != null) {
			bis_path.setSwh_avl_flg(iaryB.getSwh_avl_flg());
		}
		if (iaryB.getWip_bank_flg() != null) {
			bis_path.setWip_bank_flg(iaryB.getWip_bank_flg());
		}

		bisPathDao.update(bis_path);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void deletePathItemFnc(FbpbispthI inTrx,FbpbispthO outTrx) {
		FbpbispthIB iaryB = inTrx.getIaryA().get(0).getIaryB().get(0);
		String cr_ope_no = iaryB.getCr_ope_no();
		String path_id_fk = iaryB.getPath_id_fk();
		String path_ver_fk = iaryB.getPath_ver_fk();
		Bis_pathId id = new Bis_pathId();
		id.setCr_ope_no(cr_ope_no);
		id.setPath_id_fk(path_id_fk);
		id.setPath_ver_fk(path_ver_fk);
		Bis_path bis_path = bisPathDao.get(id);
		if (bis_path != null) {
			outTrx.setRtn_code(E_FBPBISPTH_ITEM_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("recorder already exist");
			return;
		}
		bisPathDao.delete(bis_path);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void listWholeFnc(FbpbispthO outTrx) {
		List<FbpbispthOA> oaryAList = new ArrayList<FbpbispthOA>();
		String hql = "FROM Bis_pathi";
		List<Bis_pathi> pathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : pathiList) {
			FbpbispthOA oary = new FbpbispthOA();
			oary.setPath_id(bis_pathi.getId().getPath_id());
			oary.setPath_ver(bis_pathi.getId().getPath_ver());
			oary.setPath_cate(bis_pathi.getPath_cate());
			oary.setPath_dsc(bis_pathi.getPath_dsc());
			oary.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oaryAList.add(oary);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void errActionFnc(FbpbispthO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	public void querySwhPath(FbpbispthO outTrx) {
		List<FbpbispthOA> oaryAList = new ArrayList<FbpbispthOA>();
		String hql = "FROM Bis_pathi where path_cate = '"+PATH_TYPE_REWK+"'";
		List<Bis_pathi> pathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : pathiList) {
			FbpbispthOA oary = new FbpbispthOA();
			oary.setPath_id(bis_pathi.getId().getPath_id());
			oary.setPath_ver(bis_pathi.getId().getPath_ver());
			oary.setPath_cate(bis_pathi.getPath_cate());
			oary.setPath_dsc(bis_pathi.getPath_dsc());
			oary.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oaryAList.add(oary);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}
	
	public void queryWholeSwhPath(FbpbispthO outTrx) {
		List<FbpbispthOA> oaryAList = new ArrayList<FbpbispthOA>();
		String hql = "FROM Bis_pathi where path_cate <> '"+PATH_TYPE_MAIN+"'";
		List<Bis_pathi> pathiList = bisPathiDao.find(hql);
		for (Bis_pathi bis_pathi : pathiList) {
			FbpbispthOA oary = new FbpbispthOA();
			oary.setPath_id(bis_pathi.getId().getPath_id());
			oary.setPath_ver(bis_pathi.getId().getPath_ver());
			oary.setPath_cate(bis_pathi.getPath_cate());
			oary.setPath_dsc(bis_pathi.getPath_dsc());
			oary.setFirst_ope_no(bis_pathi.getFirst_ope_no());
			oaryAList.add(oary);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt(oaryAList.size());
	}
	
	public List<Bis_pathi> queryPathiByChartId(Integer chartId){
		
		String sql="From Bis_pathi where path_cate='MAIN' and chart_id ="+chartId;
		List<Bis_pathi> pathis=bisPathiDao.find(sql);
		return pathis;
	}
	
	public void addPathi(Integer chartId,FbpbispthI inTrx,FbpbispthO outTrx){
		long tbl_cnt_a=inTrx.getTbl_cnt_a();
		
		for (int i = 0; i < tbl_cnt_a; i++) {
			FbpbispthIA iaryA = inTrx.getIaryA().get(i);	
			
			String path_id = iaryA.getPath_id();
			String path_ver = iaryA.getPath_ver();
			Bis_pathiId id = new Bis_pathiId();
			id.setPath_id(path_id);
			id.setPath_ver(path_ver);
			Bis_pathi query_bis_pathi = bisPathiDao.get(id);
			if (query_bis_pathi != null) {
				outTrx.setRtn_code(E_FBPBISPTH_ALREADY_EXIST + "");
				outTrx.setRtn_mesg("record already exist");
				return;
			}
			Bis_pathi bis_pathi = new Bis_pathi();
			bis_pathi.setId(id);
			bis_pathi.setPath_cate(iaryA.getPath_cate());
			bis_pathi.setPath_dsc(iaryA.getPath_dsc());
			bis_pathi.setFirst_ope_no(iaryA.getFirst_ope_no());
			bisPathiDao.save(bis_pathi);
			
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void saveOrUpdatePathItemFunc(FbpbispthI inTrx,FbpbispthO outTrx){
		long tbl_cnt_a=inTrx.getTbl_cnt_a();
		
		for (int j = 0; j < tbl_cnt_a; j++) {
			FbpbispthIA iaryA=inTrx.getIaryA().get(j);
			
			// 将站点信息重新写入path表
			long tbl_cnt_b = inTrx.getIaryA().get(0).getTbl_cnt_b();
			for (int i = 0; i < tbl_cnt_b; i++) {
				
				List<FbpbispthIB> iaryBList=iaryA.getIaryB();
				String path_cate=iaryA.getPath_cate();
				String swh_path_id=iaryA.getPath_id();
				String swh_path_ver=iaryA.getPath_ver();
				String swh_path_dsc=iaryA.getPath_dsc();
				if (!path_cate.equals("MAIN")) {
				    savePathSwhFunc(iaryBList,swh_path_id,swh_path_ver,swh_path_dsc);	
				}
				
				for (FbpbispthIB iaryB : iaryBList) {

					String path_id_fk = iaryB.getPath_id_fk();
					String path_ver_fk = iaryB.getPath_ver_fk();
					
					if (!path_cate.equals("MAIN")) {
						if (iaryB.getPv_ope_no().equals("FRST")|| iaryB.getNx_ope_no().equals("LAST")) {
							continue;
						}
					}
					
					// 将route 站点信息删除
					/*String hql = "FROM Bis_path where path_id_fk='" + path_id_fk + "'" + " and path_ver_fk ='" + path_ver_fk + "'";
					List<Bis_path> pathItemList = bisPathDao.find(hql);
					for (Bis_path bis_path : pathItemList) {
						bisPathDao.delete(bis_path);
					}*/
					
					String cr_ope_no = iaryB.getCr_ope_no();
					Bis_pathId id = new Bis_pathId();
					id.setCr_ope_no(cr_ope_no);
					id.setPath_id_fk(path_id_fk);
					id.setPath_ver_fk(path_ver_fk);
					Bis_path query_bis_path = bisPathDao.get(id);
					if (query_bis_path != null) {
						outTrx.setRtn_code(E_FBPBISPTH_ITEM_ALREADY_EXIST + "");
						outTrx.setRtn_mesg("recorder already exist");
						return;
					}
					Bis_path bis_path = new Bis_path();
					bis_path.setId(id);
					bis_path.setCr_ope_id_fk(iaryB.getCr_ope_id_fk());
					bis_path.setCr_ope_ver_fk(iaryB.getCr_ope_ver_fk());
					bis_path.setDft_wip_bank_tool_id_fk(iaryB.getDft_wip_bank_tool_id_fk());
					bis_path.setNx_ope_no(iaryB.getNx_ope_no());
					bis_path.setPv_ope_no(iaryB.getPv_ope_no());
					bis_path.setSwh_avl_flg(iaryB.getSwh_avl_flg());
					bis_path.setWip_bank_flg(iaryB.getWip_bank_flg());
					bisPathDao.save(bis_path);
				}
			}
			
		}
		
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	public void savePathSwhFunc(List<FbpbispthIB> iaryBList,String swh_path_id,String swh_path_ver,String swh_path_dsc){
		String fr_path_id="",fr_path_ver="",fr_ope_no="",out_path_id="",out_path_ver="",out_ope_no="";
		
		for (FbpbispthIB iaryB : iaryBList) {
			if (iaryB.getPv_ope_no().equals("FRST")) {
				fr_path_id=iaryB.getPath_id_fk();
				fr_path_ver=iaryB.getPath_ver_fk();
				fr_ope_no=iaryB.getCr_ope_no();	
			}
			
			if (iaryB.getNx_ope_no().equals("LAST")) {
				out_path_id=iaryB.getPath_id_fk();
				out_path_ver=iaryB.getPath_ver_fk();
				out_ope_no=iaryB.getCr_ope_no();
			} 
		}
		
		Bis_path_swhId id=new Bis_path_swhId();
		id.setFr_path_id_fk(fr_path_id);
		id.setFr_path_ver_fk(fr_path_ver);
		id.setFr_ope_no_fk(fr_ope_no);
		
		BigInteger count=queryPathSwhCnt(fr_path_id,fr_path_ver,fr_ope_no);
		if (count.intValue() == 0) {
			id.setSwh_id("1");
		} else {
			id.setSwh_id((count.intValue()+1)+"");
		}
		id.setSwh_typ("R");
		
		Bis_path_swh pathSwh=new Bis_path_swh();
		pathSwh.setId(id);
		pathSwh.setSwh_path_id_fk(swh_path_id);
		pathSwh.setSwh_path_ver_fk(swh_path_ver);
		pathSwh.setSwh_dsc(swh_path_dsc);
		pathSwh.setOut_path_id_fk(out_path_id);
		pathSwh.setOut_path_ver_fk(out_path_ver);
		pathSwh.setOut_ope_no_fk(out_ope_no);
		
		bisPathSwhDao.save(pathSwh);
	}
	
	public BigInteger queryPathSwhCnt(String fr_path_id,String fr_path_ver,String fr_ope_no){
		String sql="select count(*) from bis_path_swh where fr_path_id_fk='"+fr_path_id+"' and  fr_path_ver_fk='"+fr_path_ver+"' and fr_ope_no_fk='"+fr_ope_no+"' and swh_typ='R'";
		Session session = bisPathSwhDao.getCrSession();
		SQLQuery query = session.createSQLQuery(sql);
		BigInteger count  =(BigInteger) query.uniqueResult();
		return count;
	}
	
	
}
